package lntu.cn.cxx1005;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.provider.MediaStore;
import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//找到头部文本
        tvTitle =(TextView) findViewById(R.id.tv_titile);
        //页面初始化的时候，需要载入一个页面
        init();

        //找到按钮，为按钮添加监听
        RadioGroup rg=(RadioGroup)findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb=(RadioButton) group.findViewById(checkedId);
                String str=rb.getText().toString();
                tvTitle.setText(str);
                //载入Fragment1,四步
                //第一步，创建FragmentManager对象
                FragmentManager fm=getFragmentManager();
                //创建FragmentTransaction
                FragmentTransaction ft=fm.beginTransaction();
                //载入

                //判断哪一个按钮被点击了
                switch (checkedId){
                    case R.id.btn1:
                        ft.replace(R.id.fl_main,new Fragment1());
                        break;

                    case R.id.btn2:
                        ft.replace(R.id.fl_main,new Fragment2());
                        break;

                    case R.id.btn3:
                        ft.replace(R.id.fl_main,new Fragment3());
                        break;

                    case R.id.btn4:
                        ft.replace(R.id.fl_main,new Fragment4());
                        break;
                }
                //开启事务
                ft.commit();
            }
        });


    }
    private void init(){
        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.replace(R.id.fl_main,new Fragment1());
        ft.commit();
    }
}
